using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Sedna.Core;
using Sedna.Core.Common.Objects.Navigation;
using Iesi.Collections;

namespace Sedna.Util
{
    public class MultiplePropertyComparer: IComparer
    {
        private IList sortInfo;
        
        public MultiplePropertyComparer(IList sortInfo)
        {
            this.sortInfo = sortInfo;
        }

        public int Compare(Object x, Object y)
        {
            int result = 0;
            foreach (SortInfo si in sortInfo)
            {
                Object valueX = ScopePathParser.Instance.Parse(x, si.SortField).Value;
                Object valueY = ScopePathParser.Instance.Parse(y, si.SortField).Value;

                if ((valueX is IComparable) && valueY != null)
                {
                    result = (si.IsAsc ? 1 : -1) * ((IComparable)valueX).CompareTo(valueY);
                }
                else if (valueX == null || valueY == null)
                {
                    result =  (si.IsAsc ? 1 : -1) * ((valueX == null && valueY != null ? -1 : 0) + (valueX != null && valueY == null ? 1 : 0));
                }
                if (result != 0)
                    return result;
            }
            return result;
        }
    }
}
